package cn.apthink.schedule;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Objects;

import cn.apthink.utility.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PlanKPI {
	private String planId;
	private String departmentId;
	private LocalDate earliestStartDate;
	private LocalDate latestEndDate;
	private LocalDate startDate;
	private LocalDate endDate;
	private double efficiency;
	private Boolean maxEfficiency;
	private boolean continueProduct;
	private long score = 100;

	/**
	 * 比最晚交货期延迟天数
	 * 
	 * @return
	 */
	public int getDelayDay() {
		return (int) latestEndDate.until(endDate, ChronoUnit.DAYS);
	}

	public boolean isDelay() {
		return endDate.isAfter(latestEndDate);
	}

	/**
	 * 比最早开工期提前天数
	 * 
	 * @return
	 */
	public int getEarliestDay() {
		return (int) startDate.until(earliestStartDate, ChronoUnit.DAYS);
	}

	public boolean isEarliest() {
		return startDate.isBefore(earliestStartDate);
	}

	public static PlanKPI testData() {
		PlanKPI data = new PlanKPI();
		data.planId = "pl1";
		data.continueProduct = false;
		data.departmentId = "line1";
		data.earliestStartDate = LocalDate.now();
		data.latestEndDate = LocalDate.now().plusDays(10);
		data.startDate = LocalDate.now().minusDays(2);
		data.endDate = LocalDate.now().plusDays(11);
		data.efficiency = 0.75;
		return data;
	}

	@Override
	public String toString() {
		return "[" + planId + "," + departmentId + "," + score + "," + efficiency + "," + continueProduct + ","
				+ isEarliest()+ ","+ isDelay()+"("+startDate.format(DateUtil.df5)+"-"+ endDate.format(DateUtil.df5) + ")" 
				 +"("+earliestStartDate.format(DateUtil.df5)+"-"+ latestEndDate.format(DateUtil.df5) + ")" + getEarliestDay() + ","+ getDelayDay() + "]";
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PlanKPI other = (PlanKPI) obj;
		return Objects.equals(departmentId, other.departmentId) && Objects.equals(planId, other.planId);
	}

	@Override
	public int hashCode() {
		return Objects.hash(departmentId, planId);
	}

}
